/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package uit.ieproject.dataaccess;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import uit.ieproject.utilites.DbConnection;
import uit.ieproject.utilites.Info;

/**
 *
 * @author Maivia
 */
public class GroupRegexDAO {

    final String TABLE = "GroupRegex";

    public ArrayList<String> getNameAtType(String type) throws SQLException {
        ArrayList<String> groups = new ArrayList<>();
        Connection con = DbConnection.getConnection();       
       // String sql = "SELECT name FROM " + TABLE + " WHERE type LIKE +N'" + type + "'";
        StringBuffer sql = new StringBuffer();
        sql.append("SELECT name FROM ");
        sql.append(Info.TABLE_GROUP);
        sql.append(" WHERE type LIKE ?"); 
        PreparedStatement stmt = con.prepareStatement(sql.toString());
        stmt.setString(1, type);
        ResultSet rs = stmt.executeQuery();
        while (rs.next()) {
            String name = rs.getNString("name");
            groups.add(name);
        }
        rs.close();
        stmt.close();
        con.close();
        return groups;
    }

    public String getNameGroup(String name, String type) throws SQLException {
        String value;
        Connection con = DbConnection.getConnection();
        //Statement stmt = con.createStatement();
        //String sql = "SELECT groupname FROM " + TABLE + " WHERE name LIKE +N'" + name + "' AND type LIKE +N'" + type + "'";
        StringBuffer sql = new StringBuffer();
        sql.append("SELECT groupname FROM ");
        sql.append(Info.TABLE_GROUP);
        sql.append(" WHERE name LIKE ?");
        sql.append(" AND type LIKE ?");
        PreparedStatement stmt = con.prepareStatement(sql.toString());
        stmt.setString(1, name);
        stmt.setString(2, type);
        ResultSet rs = stmt.executeQuery();
        rs.next();
        value = rs.getNString("groupname");
        rs.close();
        stmt.close();
        con.close();
        return value;
    }
}
